package com.yfuxdeer.youchat.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yfuxdeer.youchat.R;
import com.yfuxdeer.youchat.pojo.User;
import com.yfuxdeer.youchat.service.UserService;
import com.yfuxdeer.youchat.ui.RoundImageView;
import com.yfuxdeer.youchat.utils.ImageUtil;
import com.yfuxdeer.youchat.utils.StorageUtil;
import com.yfuxdeer.youchat.utils.Store;
import com.yfuxdeer.youchat.utils.PermissionRequester;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = LoginActivity.class.getSimpleName();
    private RoundImageView headImage;
    private EditText username;
    private EditText password;
    private Button login_btn;
    private Button register;
    private Store store;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        findViews();
        setListeners();
        //请求权限
        PermissionRequester.requestPermission(this, new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.CAMERA,
                Manifest.permission.READ_EXTERNAL_STORAGE
        });
        init();
    }

    private void init() {

        //注册广播
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.yfuxdeer.youchat.loginReceiver");
        registerReceiver(loginReceiver, filter);

        store = Store.getInstance();
        User user = StorageUtil.readUser(getApplicationContext());
        if (user == null) {
            ImageUtil.glideLoad(getApplicationContext(), "default", store.cacheState, headImage);
            return;
        } else {
            username.setText(user.getUsername());
            password.setText(user.getPassword());
            ImageUtil.glideLoad(getApplicationContext(), user.getUsername(), store.cacheState, headImage);
        }
    }

    private void findViews() {
        headImage = findViewById(R.id.head);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        login_btn = findViewById(R.id.login);
        register = findViewById(R.id.register);
    }

    private void setListeners() {
        login_btn.setClickable(true);
        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "点击了登录按钮");
                String username_ = username.getText().toString();
                String password_ = password.getText().toString();

                //调用服务登录
                Intent intent = new Intent(LoginActivity.this, UserService.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("method", UserService.METHOD.LOGIN);
                bundle.putString("username", username_);
                bundle.putString("password", password_);
                intent.putExtras(bundle);
                startService(intent);
                Log.e(TAG, "onClick: " + "username : " + username_ + " password : " + password_);
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(), RegisterActivity.class));
            }
        });
    }


    //广播接收器
    private BroadcastReceiver loginReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            UserService.RESULT result = (UserService.RESULT) bundle.getSerializable("result");
            Log.e(TAG, "onReceive: result : " + result);
            switch (result) {
                case SUCCESS:
                    User loginUser = (User) bundle.getSerializable("loginUser");
                    StorageUtil.writeUser(getApplicationContext(), loginUser);
                    Store.getInstance().setCurrentUser(loginUser);
                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                    break;
                case NETWORK_EXCEPTION:
                    Toast.makeText(LoginActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    break;
                case ERROR:
                    Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}